<template>
  <div class="app-container">
    <!-- 添加或修改组织对话框 -->
    <el-form ref="form" :model="form1" :rules="rules" disabled label-width="95px">
      <!-- <el-row>
        <el-col :span="24" v-if="form.parentId !== 0">
          <el-form-item label="上级组织" prop="parentId">
            <treeselect v-model="form1.parentId" :disabled='true' :options="deptOptions" :normalizer="normalizer"
              placeholder="选择上级组织" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="组织简称" prop="deptName">
            <el-input v-model="form1.deptName" placeholder="组织简称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示排序" prop="orderNum">
            <el-input-number v-model="form1.orderNum" controls-position="right" :min="0" style="width: 100%;" />
          </el-form-item>
        </el-col>
      </el-row>-->

      <el-row>
        <el-col :span="12">
          <el-form-item label="建设项目" prop="constructionProject">
            <el-input v-model="form1.constructionProject" placeholder="建设项目" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检测单位" prop="testingCompany">
            <el-input v-model="form1.testingCompany" placeholder="检测单位" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="施工/委托单位" prop="constructionCompany">
            <el-input v-model="form1.constructionCompany" placeholder="施工/委托单位" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人" prop="leader">
            <el-input v-model="form1.leader" placeholder="负责人" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="监理单位" prop="supervisionCompany">
            <el-input v-model="form1.supervisionCompany" placeholder="监理单位" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="form1.phone" placeholder="联系电话" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="部门状态">
            <el-radio-group v-model="form1.status">
              <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">
                {{ dict.label
                }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否为试验室" prop="labFlag">
            <el-radio-group v-model="form1.labFlag">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="等级证书" prop="gradeCard">
            <el-input v-model="form1.gradeCard" placeholder="等级证书" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="证书编号" prop="cardNum">
            <el-input v-model="form1.cardNum" placeholder="证书编号" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="设立日期" prop="setDate">
            <el-date-picker v-model="form1.setDate" type="date" placeholder="选择日期" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="试验检测工程师(人)" prop="engineerNum" class="narrow-spacing">
            <el-input v-model="form1.engineerNum" placeholder="数量" type="Number" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="试验检测员(人)" prop="checkPersonNum" class="narrow-spacing">
            <el-input v-model="form1.checkPersonNum" type="Number" placeholder="数量" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="试验检测房总面积(㎡)" prop="checkArea" class="narrow-spacing">
            <el-input v-model="form1.checkArea" placeholder="总面积" type="Number" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="授权机构">
            <el-input v-model="form1.sqGroup" placeholder="授权机构" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" type="primary" @click="handleUpdate" :disabled="editAble">编 辑</el-button>
    </div>

    <jy-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="建设项目" prop="constructionProject">
              <el-input v-model="form.constructionProject" placeholder="建设项目" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测单位" prop="testingCompany">
              <el-input v-model="form.testingCompany" placeholder="检测单位" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="施工/委托单位" prop="constructionCompany">
              <el-input v-model="form.constructionCompany" placeholder="施工/委托单位" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="负责人" prop="leader">
              <el-input v-model="form.leader" placeholder="负责人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="监理单位" prop="supervisionCompany">
              <el-input v-model="form.supervisionCompany" placeholder="监理单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="联系电话" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="部门状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否为试验室" prop="labFlag">
              <el-radio-group v-model="form.labFlag">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="等级证书" prop="gradeCard">
              <el-input v-model="form.gradeCard" placeholder="等级证书" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="证书编号" prop="cardNum">
              <el-input v-model="form.cardNum" placeholder="证书编号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="设立日期" prop="setDate">
              <el-date-picker v-model="form.setDate" type="date" placeholder="选择日期" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="试验检测工程师(人)" prop="engineerNum" class="narrow-spacing">
              <el-input v-model="form.engineerNum" placeholder="数量" type="Number" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="试验检测员(人)" prop="checkPersonNum" class="narrow-spacing">
              <el-input v-model="form.checkPersonNum" type="Number" placeholder="数量" maxlength="20" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="试验检测房总面积(㎡)" prop="checkArea" class="narrow-spacing">
              <el-input v-model="form.checkArea" placeholder="总面积" type="Number" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="授权机构">
              <el-input v-model="form.sqGroup" placeholder="授权机构" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="small" @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listDept, getDept, addDept, updateDept, listDeptExcludeChild } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { mapGetters } from "vuex";

export default {
  name: "DeptInfo",
  dicts: ["sys_normal_disable"],
  components: { Treeselect },
  data() {
    return {
      editAble: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 组织树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        deptName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      form1: {},
      // 表单校验
      rules: {
        parentId: [{ required: true, message: "上级部门不能为空", trigger: "blur" }],
        deptName: [{ required: true, message: "部门名称不能为空", trigger: "blur" }],
        orderNum: [{ required: true, message: "显示排序不能为空", trigger: "blur" }],
        email: [
          {
            type: "email",
            message: "正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(["deptId"])
  },
  watch: {
    deptId(newValue, oldValue) {
      this.queryParams.deptId = newValue;
      this.getDept();
    }
  },
  created() {
    this.getDept();
  },

  methods: {
    /** 查询组织列表 */
    // getList() {
    //   this.loading = true;
    //   listDept(this.queryParams).then(response => {
    //     this.deptList = this.handleTree(response.data, "deptId");
    //     this.loading = false;
    //   });
    // },
    /** 转换组织数据结构 */
    normalizer(node) {
      localStorage.getItem;
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deptId: undefined,
        parentId: undefined,
        deptName: undefined,
        orderNum: undefined,
        leader: undefined,
        phone: undefined,
        email: undefined,
        status: "0"
      };
      this.resetForm("form");
    },

    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.parentId = row.deptId;
      }
      this.open = true;
      this.title = "添加部门";
      listDept().then((response) => {
        this.deptOptions = this.handleTree(response.data, "deptId");
      });
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    getDept() {
      let deptId = this.queryParams.deptId ? this.queryParams.deptId : this.$store.state.dept.deptId;
      if (deptId == null || deptId == "null" || deptId == undefined || deptId == "") {
        this.editAble = true;
      } else {
        this.editAble = false;
        getDept(deptId).then((response) => {
          this.form1 = response.data;
          // console.log("form1..............", JSON.stringify(this.form1));
          this.form1.labFlag = this.form1.labFlag ? this.form1.labFlag + "" : "0";
          listDeptExcludeChild(deptId).then((response) => {
            this.deptOptions = this.handleTree(response.data, "deptId");
            if (this.deptOptions.length == 0) {
              const noResultsOptions = { deptId: this.form.parentId, deptName: this.form.parentName, children: [] };
              this.deptOptions.push(noResultsOptions);
            }
          });
        });
      }
    },
    /** 修改按钮操作 */
    handleUpdate() {
      // this.reset();
      let deptId = this.queryParams.deptId ? this.queryParams.deptId : this.$store.state.dept.deptId;
      getDept(deptId).then((response) => {
        this.form = response.data;
        this.form.labFlag = this.form.labFlag ? this.form.labFlag + "" : "0";
        this.open = true;
        this.title = "修改试验室信息";
        listDeptExcludeChild(deptId).then((response) => {
          this.deptOptions = this.handleTree(response.data, "deptId");
          if (this.deptOptions.length == 0) {
            const noResultsOptions = { deptId: this.form.parentId, deptName: this.form.parentName, children: [] };
            this.deptOptions.push(noResultsOptions);
          }
        });
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.deptId != undefined) {
            updateDept(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getDept();
              this.$store.dispatch("getDeptInfo", this.form.deptId);
            });
          } else {
            addDept(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getDept();
            });
          }
        }
      });
    }
    /** 删除按钮操作 */
    // handleDelete(row) {
    //   this.$modal.confirm('是否确认删除名称为"' + row.deptName + '"的数据项？').then(function() {
    //     return delDept(row.deptId);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {});
    // }
  }
};
</script>
